package controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.FollowPlan;
import model.Incident;
import model.Person;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import service.Userservice;

@Controller
@RequestMapping("/manager")
public class MainManagerController {
	private String[] name = { "Anna", "Brittany", "Cinderella", "Diana", "Eva",
			"Fiona", "Gunda", "Hege", "Inga", "Johanna", "Kitty", "Linda",
			"Nina", "Ophelia", "Petunia", "Amanda", "Raquel", "Cindy", "Doris",
			"Eve", "Evita", "Sunniva", "Tove", "Unni", "Violet", "Liza",
			"Elizabeth", "Ellen", "Wenche", "Vicky" };
	
	@Resource(name="userService")
	private Userservice userservice;
	
	
	@RequestMapping(value = "all.do", method = RequestMethod.GET)
	public String findAll(Model model){
		List<Person> user = userservice.readPerson();
		List<Incident> incident = userservice.readIncident();
		List<FollowPlan> plan = userservice.readPlan();
		
		model.addAttribute("person",user);
		model.addAttribute("incident",incident);
		model.addAttribute("plan",plan);
		
		return "account/FindAll";
 	}
	
	@RequestMapping(value="vailation.do",method = RequestMethod.GET)
	public void check(HttpServletRequest request,HttpServletResponse Response) throws IOException{
 		int psd = Integer.parseInt(request.getParameter("psd"));
		PrintWriter pw = Response.getWriter();
		System.out.print(psd);
		if(psd == 1234){
			System.out.print("++");
			pw.write("noPeople");
		}
		else if(psd == 4321){
			System.out.print("--");
			pw.write("fail");
		}
		else{
			System.out.print("__");
			pw.write("success");
		}
	}
	
	@RequestMapping(value="hit.do",method = RequestMethod.GET)
 	public void hint(HttpServletRequest request,HttpServletResponse Response) throws IOException{
		String _input = request.getParameter("str");
		PrintWriter pw = Response.getWriter();
		System.out.print(_input+"====");
		String finalHit="";
		for(int i=0;i<name.length;i++){
			String temp = _input.toLowerCase();
			String hit = name[i];
			String _temp = hit.substring(0, 1);
			if(temp.equals(_temp.toLowerCase())){;
				finalHit =finalHit +"     " +hit;
			}
 		}
		
		pw.write(finalHit);
	}
/*----------------------------------------------------*/
	@RequestMapping(value = "editPerson.do", method = RequestMethod.GET)
 	public String editPerson(HttpServletRequest request,Model model){
		HttpSession session=request.getSession(true);	
		int id = Integer.parseInt(request.getParameter("id"));
		Person person = userservice.findPerson(id);
		session.setAttribute("person",person);
		return "account/EditPerson";
}
	
	@RequestMapping(value = "editIncident.do", method = RequestMethod.GET)
 	public String editIncident(HttpServletRequest request,Model model){
		HttpSession session=request.getSession(true);	
		int id = Integer.parseInt(request.getParameter("id"));
		Incident incident = userservice.findIncident(id);
		session.setAttribute("incident",incident);
		return "account/EditIncident";
  	}
	
	@RequestMapping(value = "editPlan.do", method = RequestMethod.GET)
 	public String editPlan(HttpServletRequest request,Model model){
		HttpSession session=request.getSession(true);	
		int id = Integer.parseInt(request.getParameter("id"));
		FollowPlan FollowPlan = userservice.findPlan(id);
		session.setAttribute("followPlan",FollowPlan);
		return "account/EditPlan";
  	}
/*------------------------------------------------------------------------*/
	/**
	 * person 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "editPersonTrue.do", method = RequestMethod.GET)
 	public String editPersonTrue(HttpServletRequest request){
 		String type=request.getParameter("type");
		int id = Integer.parseInt(request.getParameter("id"));
		String info = request.getParameter("info");
		userservice.updatePerson(type, id, info);
		return "account/SuccessEdit";
 	}

	@RequestMapping(value = "deletePersonTrue.do", method = RequestMethod.GET)
 	public String deletePersonTrue(HttpServletRequest request){
		int id = Integer.parseInt(request.getParameter("id"));
		userservice.deletePerson(id);
		return "account/SuccessEdit";
 	}
	
	
	
	/**
	 * incident
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "editIncidentTrue.do", method = RequestMethod.GET)
 	public String editIncidentTrue(HttpServletRequest request){
 		String type=request.getParameter("type");
		int id = Integer.parseInt(request.getParameter("id"));
		String info = request.getParameter("info");
		userservice.updateIncident(type, id, info);
		return "account/SuccessEdit";
 	}

	@RequestMapping(value = "deleteIncidentTrue.do", method = RequestMethod.GET)
 	public String deleteIncidentTrue(HttpServletRequest request){
		int id = Integer.parseInt(request.getParameter("id"));
		userservice.deleteIncident(id);
		return "account/SuccessEdit";
 	}
	
	/**
	 * plan
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "editPlanTrue.do", method = RequestMethod.GET)
 	public String editPlanTrue(HttpServletRequest request){
 		String type=request.getParameter("type");
		int id = Integer.parseInt(request.getParameter("id"));
		String info = request.getParameter("info");
		userservice.updatePlan(type, id, info);
		return "account/SuccessEdit";
 	}

	@RequestMapping(value = "deletePlanTrue.do", method = RequestMethod.GET)
 	public String deletePlanTrue(HttpServletRequest request){
		int id = Integer.parseInt(request.getParameter("id"));
		userservice.deletePlan(id);
		return "account/SuccessEdit";
 	}
 }
